package cn.congine.wizarpos.mall.action;

import java.util.Arrays;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;

import cn.congine.wizarpos.mall.model.MrtIbeacon;
import cn.congine.wizarpos.mall.model.MrtTicketDef;
import cn.congine.wizarpos.mall.model.open.MrtIbeaconInput;
import cn.congine.wizarpos.mall.model.open.MrtIbeaconOutput;
import cn.congine.wizarpos.mall.model.open.TicketDefInfoInput;
import cn.congine.wizarpos.mall.model.open.TicketDefInfoOutput;
import cn.congine.wizarpos.mall.model.open.WeChartMemberInfoQueryInput;
import cn.congine.wizarpos.mall.model.open.WeChartMemberInfoQueryOutput;
import cn.congine.wizarpos.mall.service.impl.WPosHttpClient;
import cn.congine.wizarpos.mall.service.impl.WizarposOpenRestClient;
import cn.congine.wizarpos.mall.service.impl.WxHttpClient;
import cn.congine.wizarpos.mall.utils.DES;
import cn.congine.wizarpos.mall.utils.SysConstants;

@Controller
@RequestMapping(value = "/oauth")
public class OAuthAction {

	private final Log log = LogFactory.getLog(getClass());

	@Resource(name = "messageSource")
	private MessageSource messageSource;

	@Autowired
	private WxHttpClient wxHttpClient;

	@Autowired
	private WPosHttpClient wposHttpClient;
	
	@Autowired
	private WizarposOpenRestClient wizarposOpenRestClient;

	// 单张券
	public static final String SIGLE = "A";

	// 多张券
	public static final String MULTIPLE = "B";

	// 广告投放
	public static final String ADVERTISEMENT = "C";

	// 未关注
	public static final String UNSUBSCRIBE = "0";

	/**
	 * 货架
	 * 
	 * @param code
	 * @param state
	 * @return
	 */
	@RequestMapping(value = "/shelf")
	public String index(@RequestParam(value = "code") String code,
			@RequestParam(value = "state", required = false) String state,
			HttpServletRequest httpRequest) {
		log.info("code=" + code + ",state=" + state);
		String openid = null;
		try {
			openid = wxHttpClient.getOpenId(code, state);
		} catch (Exception e) {
			return "redirect:/oauth/errmsg?msg=" + e.getMessage();
		}
		log.info("openId is " + openid);
		if (openid == null) {
			return "redirect:/oauth/error?type=1";
		}
		
		httpRequest.getSession().setAttribute("openId", openid);
		httpRequest.getSession().setAttribute("mid", state);
		
		// 请求开放平台查询会员卡信息
		WeChartMemberInfoQueryInput input = new WeChartMemberInfoQueryInput();
		input.setMid(state);
		input.setOpenId(openid);
		WeChartMemberInfoQueryOutput output = (WeChartMemberInfoQueryOutput) wizarposOpenRestClient.post(input, "/wechart/info/query", WeChartMemberInfoQueryOutput.class);
		if (output != null && "0".equals(output.getCode())) {
			WeChartMemberInfoQueryOutput.Result result = output.getResult();
			if (result != null) {
				httpRequest.getSession().setAttribute("mrtCardNo", result.getCardNo());
				httpRequest.getSession().setAttribute("mrtCardId",  result.getId());	
				if (StringUtils.isEmpty(result.getUserName()) || StringUtils.isEmpty(result.getMobileNo())) {
					// 会员卡存在未激活，用户名和手机号有空值，激活会员卡
					return "redirect:/vip/receiveCard?mid=" + state;
				}
			}
		}

		return "redirect:/index/index?mid=" + state;
	}

	/**
	 * 东志信货架
	 * 
	 * @param code
	 * @param state
	 * @return
	 */
	@RequestMapping(value = "/index")
	public String dgdIndex(@RequestParam(value = "code") String code,
			@RequestParam(value = "state", required = false) String state,
			HttpServletRequest httpRequest) {
		log.info("code=" + code + ",state=" + state);
		String openid = null;
		try {
			openid = wxHttpClient.getOpenId(code, state);
		} catch (Exception e) {
			return "redirect:/oauth/errmsg?msg=" + e.getMessage();
		}
		log.info("openId is " + openid);
		if (openid == null) {
			return "redirect:/oauth/error?type=1";
		}
		
		httpRequest.getSession().setAttribute("openId", openid);
		httpRequest.getSession().setAttribute("mid", state);
		
		// 请求开放平台查询会员卡信息
		WeChartMemberInfoQueryInput input = new WeChartMemberInfoQueryInput();
		input.setMid(state);
		input.setOpenId(openid);
		WeChartMemberInfoQueryOutput output = (WeChartMemberInfoQueryOutput) wizarposOpenRestClient.post(input, "/wechart/info/query", WeChartMemberInfoQueryOutput.class);
		if (output != null && "0".equals(output.getCode())) {
			WeChartMemberInfoQueryOutput.Result result = output.getResult();
			if (result != null) {
				httpRequest.getSession().setAttribute("mrtCardNo", result.getCardNo());
				httpRequest.getSession().setAttribute("mrtCardId",  result.getId());
			}
		}

		return "redirect:/index/dgd_index?mid=" + state;
	}
	
	/**
	 * 商品详情
	 * 
	 * @param code
	 * @param state   state为mid和productId的组合,以PRODUCTID分隔
	 * @param httpRequest
	 * @return
	 */
	@RequestMapping(value = "/product")
	public String productDetail(@RequestParam(value = "code") String code,
			@RequestParam(value = "state", required = false) String state,
			HttpServletRequest httpRequest) {
		log.info("code=" + code + ",state=" + state);
		int pos = state.indexOf("PRODUCTID");
		String mid = state.substring(0, pos);
		String id = state.substring(pos + 9);
		String openid = null;
		try {
			openid = wxHttpClient.getOpenId(code, mid);
		} catch (Exception e) {
			return "redirect:/oauth/errmsg?msg=" + e.getMessage();
		}
		log.info("openId is " + openid);
		if (openid == null) {
			return "redirect:/oauth/error?type=1";
		}
		
		httpRequest.getSession().setAttribute("openId", openid);
		httpRequest.getSession().setAttribute("mid", mid);
		
		// 请求开放平台查询会员卡信息
		WeChartMemberInfoQueryInput input = new WeChartMemberInfoQueryInput();
		input.setMid(mid);
		input.setOpenId(openid);
		WeChartMemberInfoQueryOutput output = (WeChartMemberInfoQueryOutput) wizarposOpenRestClient.post(input, "/wechart/info/query", WeChartMemberInfoQueryOutput.class);
		if (output != null && "0".equals(output.getCode())) {
			WeChartMemberInfoQueryOutput.Result result = output.getResult();
			if (result != null) {
				httpRequest.getSession().setAttribute("mrtCardNo", result.getCardNo());
				httpRequest.getSession().setAttribute("mrtCardId",  result.getId());	
				if (StringUtils.isEmpty(result.getUserName()) || StringUtils.isEmpty(result.getMobileNo())) {
					// 会员卡存在未激活，用户名和手机号有空值，激活会员卡
					return "redirect:/vip/receiveCard?mid=" + mid;
				}
			}
		}


		return "redirect:/erp_product/get?mid=" + mid + "&id=" + id;
	}
	
	/**
	 * 微信订单
	 * 
	 * @param code
	 * @param state
	 * @return
	 */
	@RequestMapping(value = "/orders")
	public String getOrders(@RequestParam(value = "code") String code,
			@RequestParam(value = "state", required = false) String state,
			HttpServletRequest httpRequest) {
		String openid = null;
		try {
			openid = wxHttpClient.getOpenId(code, state);
		} catch (Exception e) {
			return "redirect:/oauth/errmsg?msg=" + e.getMessage();
		}
		log.info("openId is " + openid);
		if (openid == null) {
			return "redirect:/oauth/error?type=1";
		}
		
		httpRequest.getSession().setAttribute("openId", openid);
		httpRequest.getSession().setAttribute("mid", state);
		
		// 请求开放平台查询会员卡信息
		WeChartMemberInfoQueryInput input = new WeChartMemberInfoQueryInput();
		input.setMid(state);
		input.setOpenId(openid);
		WeChartMemberInfoQueryOutput output = (WeChartMemberInfoQueryOutput) wizarposOpenRestClient.post(input, "/wechart/info/query", WeChartMemberInfoQueryOutput.class);
		if (output != null && "0".equals(output.getCode())) {
			WeChartMemberInfoQueryOutput.Result result = output.getResult();
			if (result != null) {
				httpRequest.getSession().setAttribute("mrtCardNo", result.getCardNo());
				httpRequest.getSession().setAttribute("mrtCardId",  result.getId());	
				if (StringUtils.isEmpty(result.getUserName()) || StringUtils.isEmpty(result.getMobileNo())) {
					// 会员卡存在未激活，用户名和手机号有空值，激活会员卡
					return "redirect:/vip/receiveCard?mid=" + state;
				}
			} else {
				return "redirect:/oauth/error?type=9";// 其他错误，系统异常
			}
		} else if (output == null) {// 请求开放平台失败
			return "redirect:/oauth/error?type=9";// 其他错误，系统异常
		} else if ("60001".equals(output.getCode())) {// 60001 	非法慧商户号
			return "redirect:/oauth/error?type=7";// 未知商户
		} else if ("60007".equals(output.getCode())) {// 60007	会员不存在
			//若无会员卡，则前往会员卡领取页面领取会员卡
			return "redirect:/vip/receiveCard?mid=" + state;
		} else {
			// 60002	在此商户下不存在此会员
			// 60009	入参为空
			log.error("code: " + output.getCode());
			log.error("message: " + output.getMessage());
			return "redirect:/oauth/error?type=9";// 其他错误，系统异常
		}
		
		return "redirect:/wx_order_detail/my_order_detail?mid=" + state;
	}

	/**
	 * 购物车
	 * 
	 * @param code
	 * @param state
	 * @return
	 */
	@RequestMapping(value = "/shopping_cart")
	public String shoppingCart(@RequestParam(value = "code") String code,
			@RequestParam(value = "state", required = false) String state,
			HttpServletRequest httpRequest) {
		String openid = null;
		try {
			openid = wxHttpClient.getOpenId(code, state);
		} catch (Exception e) {
			return "redirect:/oauth/errmsg?msg=" + e.getMessage();
		}
		log.info("openId is " + openid);
		if (openid == null) {
			return "redirect:/oauth/error?type=1";
		}
		
		httpRequest.getSession().setAttribute("openId", openid);
		httpRequest.getSession().setAttribute("mid", state);
		
		// 请求开放平台查询会员卡信息
		WeChartMemberInfoQueryInput input = new WeChartMemberInfoQueryInput();
		input.setMid(state);
		input.setOpenId(openid);
		WeChartMemberInfoQueryOutput output = (WeChartMemberInfoQueryOutput) wizarposOpenRestClient.post(input, "/wechart/info/query", WeChartMemberInfoQueryOutput.class);
		if (output != null && "0".equals(output.getCode())) {
			WeChartMemberInfoQueryOutput.Result result = output.getResult();
			if (result != null) {
				httpRequest.getSession().setAttribute("mrtCardNo", result.getCardNo());
				httpRequest.getSession().setAttribute("mrtCardId",  result.getId());	
				if (StringUtils.isEmpty(result.getUserName()) || StringUtils.isEmpty(result.getMobileNo())) {
					// 会员卡存在未激活，用户名和手机号有空值，激活会员卡
					return "redirect:/vip/receiveCard?mid=" + state;
				}
			} else {
				return "redirect:/oauth/error?type=9";// 其他错误，系统异常
			}
		} else if (output == null) {// 请求开放平台失败
			return "redirect:/oauth/error?type=9";// 其他错误，系统异常
		} else if ("60001".equals(output.getCode())) {// 60001 	非法慧商户号
			return "redirect:/oauth/error?type=7";// 未知商户
		} else if ("60007".equals(output.getCode())) {// 60007	会员不存在
			//若无会员卡，则前往会员卡领取页面领取会员卡
			return "redirect:/vip/receiveCard?mid=" + state;
		} else {
			// 60002	在此商户下不存在此会员
			// 60009	入参为空
			log.error("code: " + output.getCode());
			log.error("message: " + output.getMessage());
			return "redirect:/oauth/error?type=9";// 其他错误，系统异常
		}
		
		return "redirect:/wx_shopping_cart/get_cart_list?mid=" + state;
	}

	/**
	 * 提货券
	 * 
	 * @param code
	 * @param state
	 * @return
	 */
	@RequestMapping(value = "/vouchers")
	public String vouchers(@RequestParam(value = "code") String code,
			@RequestParam(value = "state", required = false) String state,
			HttpServletRequest httpRequest) {
		String openid = null;
		try {
			openid = wxHttpClient.getOpenId(code, state);
		} catch (Exception e) {
			return "redirect:/oauth/errmsg?msg=" + e.getMessage();
		}
		log.info("openId is " + openid);
		if (openid == null) {
			return "redirect:/oauth/error?type=1";
		}
		
		httpRequest.getSession().setAttribute("openId", openid);
		httpRequest.getSession().setAttribute("mid", state);
		
		// 请求开放平台查询会员卡信息
		WeChartMemberInfoQueryInput input = new WeChartMemberInfoQueryInput();
		input.setMid(state);
		input.setOpenId(openid);
		WeChartMemberInfoQueryOutput output = (WeChartMemberInfoQueryOutput) wizarposOpenRestClient.post(input, "/wechart/info/query", WeChartMemberInfoQueryOutput.class);
		if (output != null && "0".equals(output.getCode())) {
			WeChartMemberInfoQueryOutput.Result result = output.getResult();
			if (result != null) {
				httpRequest.getSession().setAttribute("mrtCardNo", result.getCardNo());
				httpRequest.getSession().setAttribute("mrtCardId",  result.getId());	
				if (StringUtils.isEmpty(result.getUserName()) || StringUtils.isEmpty(result.getMobileNo())) {
					// 会员卡存在未激活，用户名和手机号有空值，激活会员卡
					return "redirect:/vip/receiveCard?mid=" + state;
				}
			} else {
				return "redirect:/oauth/error?type=9";// 其他错误，系统异常
			}
		} else if (output == null) {// 请求开放平台失败
			return "redirect:/oauth/error?type=9";// 其他错误，系统异常
		} else if ("60001".equals(output.getCode())) {// 60001 	非法慧商户号
			return "redirect:/oauth/error?type=7";// 未知商户
		} else if ("60007".equals(output.getCode())) {// 60007	会员不存在
			//若无会员卡，则前往会员卡领取页面领取会员卡
			return "redirect:/vip/receiveCard?mid=" + state;
		} else {
			// 60002	在此商户下不存在此会员
			// 60009	入参为空
			log.error("code: " + output.getCode());
			log.error("message: " + output.getMessage());
			return "redirect:/oauth/error?type=9";// 其他错误，系统异常
		}
		
		return "redirect:/product_voucher/product_voucher_list?mid=" + state;
	}

	/**
	 * 
	 * @param code
	 * @param state
	 * @return
	 */
	@RequestMapping(value = "/cards")
	public String cards(@RequestParam(value = "code") String code,
			@RequestParam(value = "state", required = false) String state,
			HttpServletRequest httpRequest) {
		String openid = null;
		try {
			openid = wxHttpClient.getOpenId(code, state);
		} catch (Exception e) {
			return "redirect:/oauth/errmsg?msg=" + e.getMessage();
		}
		log.info("openId is " + openid);
		if (openid == null) {
			return "redirect:/oauth/error?type=1";
		}
		
		httpRequest.getSession().setAttribute("openId", openid);
		httpRequest.getSession().setAttribute("mid", state);
		
		// 请求开放平台查询会员卡信息
		WeChartMemberInfoQueryInput input = new WeChartMemberInfoQueryInput();
		input.setMid(state);
		input.setOpenId(openid);
		WeChartMemberInfoQueryOutput output = (WeChartMemberInfoQueryOutput) wizarposOpenRestClient.post(input, "/wechart/info/query", WeChartMemberInfoQueryOutput.class);
		if (output != null && "0".equals(output.getCode())) {
			WeChartMemberInfoQueryOutput.Result result = output.getResult();
			if (result != null) {
				httpRequest.getSession().setAttribute("mrtCardNo", result.getCardNo());
				httpRequest.getSession().setAttribute("mrtCardId",  result.getId());	
				if (StringUtils.isEmpty(result.getUserName()) || StringUtils.isEmpty(result.getMobileNo())) {
					// 会员卡存在未激活，用户名和手机号有空值，激活会员卡
					return "redirect:/vip/receiveCard?mid=" + state;
				}
			} else {
				return "redirect:/oauth/error?type=9";// 其他错误，系统异常
			}
		} else if (output == null) {// 请求开放平台失败
			return "redirect:/oauth/error?type=9";// 其他错误，系统异常
		} else if ("60001".equals(output.getCode())) {// 60001 	非法慧商户号
			return "redirect:/oauth/error?type=7";// 未知商户
		} else if ("60007".equals(output.getCode())) {// 60007	会员不存在
			//若无会员卡，则前往会员卡领取页面领取会员卡
			return "redirect:/vip/receiveCard?mid=" + state;
		} else {
			// 60002	在此商户下不存在此会员
			// 60009	入参为空
			log.error("code: " + output.getCode());
			log.error("message: " + output.getMessage());
			return "redirect:/oauth/error?type=9";// 其他错误，系统异常
		}
		
		return "redirect:/vip/vip_card?mid=" + state;
	}

	/**
	 * 收货地址
	 * 
	 * @param code
	 * @param state
	 * @return
	 */
	@RequestMapping(value = "/my_address")
	public String myAddress(@RequestParam(value = "code") String code,
			@RequestParam(value = "state", required = false) String state,
			HttpServletRequest httpRequest) {
		String openid = null;
		try {
			openid = wxHttpClient.getOpenId(code, state);
		} catch (Exception e) {
			return "redirect:/oauth/errmsg?msg=" + e.getMessage();
		}
		log.info("openId is " + openid);
		if (openid == null) {
			return "redirect:/oauth/error?type=1";
		}
		
		httpRequest.getSession().setAttribute("openId", openid);
		httpRequest.getSession().setAttribute("mid", state);
		// 请求开放平台查询会员卡信息
		WeChartMemberInfoQueryInput input = new WeChartMemberInfoQueryInput();
		input.setMid(state);
		input.setOpenId(openid);
		WeChartMemberInfoQueryOutput output = (WeChartMemberInfoQueryOutput) wizarposOpenRestClient.post(input, "/wechart/info/query", WeChartMemberInfoQueryOutput.class);
		if (output != null && "0".equals(output.getCode())) {
			WeChartMemberInfoQueryOutput.Result result = output.getResult();
			if (result != null) {
				httpRequest.getSession().setAttribute("mrtCardNo", result.getCardNo());
				httpRequest.getSession().setAttribute("mrtCardId",  result.getId());	
				if (StringUtils.isEmpty(result.getUserName()) || StringUtils.isEmpty(result.getMobileNo())) {
					// 会员卡存在未激活，用户名和手机号有空值，激活会员卡
					return "redirect:/vip/receiveCard?mid=" + state;
				}
			} else {
				return "redirect:/oauth/error?type=9";// 其他错误，系统异常
			}
		} else if (output == null) {// 请求开放平台失败
			return "redirect:/oauth/error?type=9";// 其他错误，系统异常
		} else if ("60001".equals(output.getCode())) {// 60001 	非法慧商户号
			return "redirect:/oauth/error?type=7";// 未知商户
		} else if ("60007".equals(output.getCode())) {// 60007	会员不存在
			//若无会员卡，则前往会员卡领取页面领取会员卡
			return "redirect:/vip/receiveCard?mid=" + state;
		} else {
			// 60002	在此商户下不存在此会员
			// 60009	入参为空
			log.error("code: " + output.getCode());
			log.error("message: " + output.getMessage());
			return "redirect:/oauth/error?type=9";// 其他错误，系统异常
		}
		
		return "redirect:/wx_address/select?mid=" + state;
	}
	
	/**
	 * 我的订单（同会员卡页我的订单）
	 * 
	 * @param code
	 * @param state
	 * @return
	 */
	@RequestMapping(value = "/myorders")
	public String myOrders(@RequestParam(value = "code") String code,
			@RequestParam(value = "state", required = false) String state,
			HttpServletRequest httpRequest) {
		String openid = null;
		try {
			openid = wxHttpClient.getOpenId(code, state);
		} catch (Exception e) {
			return "redirect:/oauth/errmsg?msg=" + e.getMessage();
		}
		log.info("openId is " + openid);
		if (openid == null) {
			return "redirect:/oauth/error?type=1";
		}
		
		httpRequest.getSession().setAttribute("openId", openid);
		httpRequest.getSession().setAttribute("mid", state);
		
		// 请求开放平台查询会员卡信息
		WeChartMemberInfoQueryInput input = new WeChartMemberInfoQueryInput();
		input.setMid(state);
		input.setOpenId(openid);
		WeChartMemberInfoQueryOutput output = (WeChartMemberInfoQueryOutput) wizarposOpenRestClient.post(input, "/wechart/info/query", WeChartMemberInfoQueryOutput.class);
		if (output != null && "0".equals(output.getCode())) {
			WeChartMemberInfoQueryOutput.Result result = output.getResult();
			if (result != null) {
				httpRequest.getSession().setAttribute("mrtCardNo", result.getCardNo());
				httpRequest.getSession().setAttribute("mrtCardId",  result.getId());	
				if (StringUtils.isEmpty(result.getUserName()) || StringUtils.isEmpty(result.getMobileNo())) {
					// 会员卡存在未激活，用户名和手机号有空值，激活会员卡
					return "redirect:/vip/receiveCard?mid=" + state;
				}
			} else {
				return "redirect:/oauth/error?type=9";// 其他错误，系统异常
			}
		} else if (output == null) {// 请求开放平台失败
			return "redirect:/oauth/error?type=9";// 其他错误，系统异常
		} else if ("60001".equals(output.getCode())) {// 60001 	非法慧商户号
			return "redirect:/oauth/error?type=7";// 未知商户
		} else if ("60007".equals(output.getCode())) {// 60007	会员不存在
			//若无会员卡，则前往会员卡领取页面领取会员卡
			return "redirect:/vip/receiveCard?mid=" + state;
		} else {
			// 60002	在此商户下不存在此会员
			// 60009	入参为空
			log.error("code: " + output.getCode());
			log.error("message: " + output.getMessage());
			return "redirect:/oauth/error?type=9";// 其他错误，系统异常
		}
		
		return "redirect:/dgd/my_orders?mid=" + state;
	}
	
	/**
	 * 门店信息
	 * 
	 * @param code
	 * @param state
	 * @return
	 */
	@RequestMapping(value = "/merchant_info")
	public String merchantInfo(@RequestParam(value = "code") String code,
			@RequestParam(value = "state", required = false) String state,
			HttpServletRequest httpRequest) {
		String openid = null;
		try {
			openid = wxHttpClient.getOpenId(code, state);
		} catch (Exception e) {
			return "redirect:/oauth/errmsg?msg=" + e.getMessage();
		}
		log.info("openId is " + openid);
		if (openid == null) {
			return "redirect:/oauth/error?type=1";
		}
		
		httpRequest.getSession().setAttribute("openId", openid);
		httpRequest.getSession().setAttribute("mid", state);
		
		// 请求开放平台查询会员卡信息
		WeChartMemberInfoQueryInput input = new WeChartMemberInfoQueryInput();
		input.setMid(state);
		input.setOpenId(openid);
		WeChartMemberInfoQueryOutput output = (WeChartMemberInfoQueryOutput) wizarposOpenRestClient.post(input, "/wechart/info/query", WeChartMemberInfoQueryOutput.class);
		if (output != null && "0".equals(output.getCode())) {
			WeChartMemberInfoQueryOutput.Result result = output.getResult();
			if (result != null) {
				httpRequest.getSession().setAttribute("mrtCardNo", result.getCardNo());
				httpRequest.getSession().setAttribute("mrtCardId",  result.getId());	
				if (StringUtils.isEmpty(result.getUserName()) || StringUtils.isEmpty(result.getMobileNo())) {
					// 会员卡存在未激活，用户名和手机号有空值，激活会员卡
					return "redirect:/vip/receiveCard?mid=" + state;
				}
			} else {
				return "redirect:/oauth/error?type=9";// 其他错误，系统异常
			}
		} else if (output == null) {// 请求开放平台失败
			return "redirect:/oauth/error?type=9";// 其他错误，系统异常
		} else if ("60001".equals(output.getCode())) {// 60001 	非法慧商户号
			return "redirect:/oauth/error?type=7";// 未知商户
		} else if ("60007".equals(output.getCode())) {// 60007	会员不存在
			//若无会员卡，则前往会员卡领取页面领取会员卡
			return "redirect:/vip/receiveCard?mid=" + state;
		} else {
			// 60002	在此商户下不存在此会员
			// 60009	入参为空
			log.error("code: " + output.getCode());
			log.error("message: " + output.getMessage());
			return "redirect:/oauth/error?type=9";// 其他错误，系统异常
		}

		return "redirect:/merchant/info?mid=" + state;
		
	}

	/**
	 * 红包入口, state是ticketDef的id
	 */
	@RequestMapping(value = "/hongbao_enter")
	public String hongbaoEnter(@RequestParam(value = "code") String code,
			@RequestParam(value = "state", required = false) String state,
			HttpServletRequest httpRequest) {
		log.info("code=" + code + ",state=" + state);		
		TicketDefInfoInput input = new TicketDefInfoInput();
		input.setTicketDefIds(Arrays.asList(state));
		TicketDefInfoOutput output = (TicketDefInfoOutput) wizarposOpenRestClient.post(input, "/wechart/ticketdef/info", TicketDefInfoOutput.class);		
		MrtTicketDef ticketDef = null;
		if (output == null || SysConstants.OPEN_ERROR.equals(output.getCode())) {
			return "redirect:/oauth/error?type=0";
		} else {
			ticketDef  = output.getResult().get(0);
		}
		
		if (ticketDef == null) {
			return "redirect:/oauth/error?type=8";

		}
		String mid = ticketDef.getMid();
		String openid = null;
		try {
			openid = wxHttpClient.getOpenId(code, mid);
		} catch (Exception e) {
			return "redirect:/oauth/errmsg?msg=" + e.getMessage();
		}

		log.info("openId is " + openid);
		if (openid == null || openid.isEmpty()) {
			return "redirect:/oauth/error?type=1";
		}

		httpRequest.getSession().setAttribute("openId", openid);
		httpRequest.getSession().setAttribute("mid", mid);

		// 请求开放平台查询会员卡信息
		WeChartMemberInfoQueryInput memberInfoQueryInput = new WeChartMemberInfoQueryInput();
		memberInfoQueryInput.setMid(mid);
		memberInfoQueryInput.setOpenId(openid);
		WeChartMemberInfoQueryOutput infoQueryOutput = (WeChartMemberInfoQueryOutput) wizarposOpenRestClient.post(input, "/wechart/info/query", WeChartMemberInfoQueryOutput.class);
		if (infoQueryOutput != null && "0".equals(infoQueryOutput.getCode())) {
			WeChartMemberInfoQueryOutput.Result result = infoQueryOutput.getResult();
			if (result != null) {
				httpRequest.getSession().setAttribute("mrtCardNo", result.getCardNo());
				httpRequest.getSession().setAttribute("mrtCardId",  result.getId());	
				if (StringUtils.isEmpty(result.getUserName()) || StringUtils.isEmpty(result.getMobileNo())) {
					// 会员卡存在未激活，用户名和手机号有空值，激活会员卡
					return "redirect:/vip/receiveCard?mid=" + mid;
				}
			} else {
				return "redirect:/oauth/error?type=9";// 其他错误，系统异常
			}
		} else if (infoQueryOutput == null) {// 请求开放平台失败
			return "redirect:/oauth/error?type=9";// 其他错误，系统异常
		} else if ("60001".equals(infoQueryOutput.getCode())) {// 60001 	非法慧商户号
			return "redirect:/oauth/error?type=7";// 未知商户
		} else if ("60007".equals(infoQueryOutput.getCode())) {// 60007	会员不存在
			return "redirect:/hongbao/register?mid=" + mid;
		} else {
			// 60002	在此商户下不存在此会员
			// 60009	入参为空
			log.error("code: " + infoQueryOutput.getCode());
			log.error("message: " + infoQueryOutput.getMessage());
			return "redirect:/oauth/error?type=9";// 其他错误，系统异常
		}
		
		if (ticketDef.getHbBonusNum() > ticketDef.getHbUseNum()) {
			return "redirect:/hongbao/detail?mid=" + mid + "&ticketId=" + ticketDef.getId();
		} else {
			return "redirect:/hongbao/no_hongbao?mid=" + mid;
		}

	}

	@RequestMapping(value = "/error")
	public ModelAndView error(int type) {
		if (1==type) {
			log.error("请求用户openid失败");
			return new ModelAndView("error").addObject("err_message",
					"请求用户openid失败,请重新操作,多次出现请联系商家");
		} else if (2==type) {
			log.error("ibeacon，场景未设置");
			return new ModelAndView("error").addObject("err_message",
					"ibeacon，场景未设置");
		} else if (3==type) {
			log.error("ibeacon，场景设置的券已失效");
			return new ModelAndView("error").addObject("err_message",
					"ibeacon，场景设置的券已失效");
		} else if (4==type) {
			log.error("ibeacon，请求用户信息失败");
			return new ModelAndView("error").addObject("err_message",
					"网络异常，请求用户信息失败");
		} else if (5==type) {
			log.error("快速注册失败");
			return new ModelAndView("error").addObject("err_message",
					"系统异常，快速注册用户失败");
		} else if (6==type) {
			log.error("ibeacon，关注我页面未设置");
			return new ModelAndView("error").addObject("err_message",
					"ibeacon，关注我页面未设置");
		} else if (7==type) {
			log.error("未知商户");
			return new ModelAndView("error").addObject("err_message",
					"未知商户");
		} else if (8==type) {
			log.error("卡券异常");
			return new ModelAndView("error").addObject("err_message",
					"卡券异常");
		}
	
		
		log.error("ibeacon，系统错误");
		return new ModelAndView("error").addObject("err_message",
				"系统错误");
		
	}
	
	@RequestMapping(value = "/errmsg")
	public ModelAndView errmsg(String msg) {
		return new ModelAndView("error").addObject("err_message",
				msg);
	}
	
	@RequestMapping(value = "/adDemo")
	public ModelAndView ibeaconAdDemo() {
		return new ModelAndView("demo/test-page");
	}
	
	/**
	 * 餐饮点单入口
	 * 
	 * @param code
	 * @param state
	 * @return
	 */
	@RequestMapping(value = "/catering")
	public String cateringEnter(@RequestParam(value = "code") String code,
			@RequestParam(value = "state", required = false) String state,
			HttpServletRequest httpRequest) {
		log.info("code=" + code + ",state=" + state);
		String openid = null;
		try {
			openid = wxHttpClient.getOpenId(code, state);
		} catch (Exception e) {
			return "redirect:/oauth/errmsg?msg=" + e.getMessage();
		}
		
		log.info("openId is " + openid);
		if (openid == null) {
			return "redirect:/oauth/error?type=1";
		}
		
		httpRequest.getSession().setAttribute("openId", openid);
		httpRequest.getSession().setAttribute("mid", state);

		// 请求开放平台查询会员卡信息
		WeChartMemberInfoQueryInput input = new WeChartMemberInfoQueryInput();
		input.setMid(state);
		input.setOpenId(openid);
		WeChartMemberInfoQueryOutput output = (WeChartMemberInfoQueryOutput) wizarposOpenRestClient.post(input, "/wechart/info/query", WeChartMemberInfoQueryOutput.class);
		if (output != null && "0".equals(output.getCode())) {
			WeChartMemberInfoQueryOutput.Result result = output.getResult();
			if (result != null) {
				httpRequest.getSession().setAttribute("mrtCardNo", result.getCardNo());
				httpRequest.getSession().setAttribute("mrtCardId",  result.getId());	
				if (StringUtils.isEmpty(result.getUserName()) || StringUtils.isEmpty(result.getMobileNo())) {
					// 会员卡存在未激活，用户名和手机号有空值，激活会员卡
					return "redirect:/vip/receiveCard?mid=" + state;
				}
			}
		}
		
		return "redirect:/dgd/tomenu?mid=" + state;
	}	
	
	/**
	 * 扫码后进入餐饮点单页
	 * 
	 * @param code
	 * @param state
	 * @return
	 */
	@RequestMapping(value = "/scan2menu")
	public String scan2menu(@RequestParam(value = "code") String code,
			@RequestParam(value = "state", required = false) String state,
			HttpServletRequest httpRequest) {
		log.info("code=" + code + ",state=" + state);
		String openid = null;
		try {
			openid = wxHttpClient.getOpenId(code, state);
		} catch (Exception e) {
			return "redirect:/oauth/errmsg?msg=" + e.getMessage();
		}
		log.info("openId is " + openid);
		if (openid == null) {
			return "redirect:/oauth/error?type=1";
		}
		
		httpRequest.getSession().setAttribute("openId", openid);
		httpRequest.getSession().setAttribute("mid", state);
		
		// 请求开放平台查询会员卡信息
		WeChartMemberInfoQueryInput input = new WeChartMemberInfoQueryInput();
		input.setMid(state);
		input.setOpenId(openid);
		WeChartMemberInfoQueryOutput output = (WeChartMemberInfoQueryOutput) wizarposOpenRestClient.post(input, "/wechart/info/query", WeChartMemberInfoQueryOutput.class);
		if (output != null && "0".equals(output.getCode())) {
			WeChartMemberInfoQueryOutput.Result result = output.getResult();
			if (result != null) {
				httpRequest.getSession().setAttribute("mrtCardNo", result.getCardNo());
				httpRequest.getSession().setAttribute("mrtCardId",  result.getId());	
				if (StringUtils.isEmpty(result.getUserName()) || StringUtils.isEmpty(result.getMobileNo())) {
					// 会员卡存在未激活，用户名和手机号有空值，激活会员卡
					return "redirect:/vip/receiveCard?mid=" + state;
				}
			} else {
				return "redirect:/oauth/error?type=9";// 其他错误，系统异常
			}
		} else if (output == null) {// 请求开放平台失败
			return "redirect:/oauth/error?type=9";// 其他错误，系统异常
		} else if ("60001".equals(output.getCode())) {// 60001 	非法慧商户号
			return "redirect:/oauth/error?type=7";// 未知商户
		} else if ("60007".equals(output.getCode())) {// 60007	会员不存在
			//若无会员卡，则前往会员卡领取页面领取会员卡
			return "redirect:/vip/receiveCard?mid=" + state;
		} else {
			// 60002	在此商户下不存在此会员
			// 60009	入参为空
			log.error("code: " + output.getCode());
			log.error("message: " + output.getMessage());
			return "redirect:/oauth/error?type=9";// 其他错误，系统异常
		}
		
		return "redirect:/catering/scan2menu?mid=" + state;
	}
	
	/**
	 * The enter of yuding for WeChart menu setting
	 * 
	 * @param code
	 * @param state
	 * @return
	 */
	@RequestMapping(value = "/yuding")
	public String yuding(@RequestParam(value = "code") String code,
			@RequestParam(value = "state", required = false) String state,
			HttpServletRequest httpRequest) {
		log.info("code=" + code + ",state=" + state);
		String openid = null;
		try {
			openid = wxHttpClient.getOpenId(code, state);
		} catch (Exception e) {
			return "redirect:/oauth/errmsg?msg=" + e.getMessage();
		}
		log.info("openId is " + openid);
		if (openid == null) {
			return "redirect:/oauth/error?type=1";
		}
		
		httpRequest.getSession().setAttribute("openId", openid);
		httpRequest.getSession().setAttribute("mid", state);
		
		// 请求开放平台查询会员卡信息
		WeChartMemberInfoQueryInput input = new WeChartMemberInfoQueryInput();
		input.setMid(state);
		input.setOpenId(openid);
		WeChartMemberInfoQueryOutput output = (WeChartMemberInfoQueryOutput) wizarposOpenRestClient.post(input, "/wechart/info/query", WeChartMemberInfoQueryOutput.class);
		if (output != null && "0".equals(output.getCode())) {
			WeChartMemberInfoQueryOutput.Result result = output.getResult();
			if (result != null) {
				httpRequest.getSession().setAttribute("mrtCardNo", result.getCardNo());
				httpRequest.getSession().setAttribute("mrtCardId",  result.getId());	
				if (StringUtils.isEmpty(result.getUserName()) || StringUtils.isEmpty(result.getMobileNo())) {
					// 会员卡存在未激活，用户名和手机号有空值，激活会员卡
					return "redirect:/vip/receiveCard?mid=" + state;
				}
			}
		}
		
		return "redirect:/catering/yudingTable?mid=" + state;
	}
	
	/**
	 * The enter of yuding for WeChart menu setting
	 * 
	 * @param code
	 * @param state
	 * @return
	 */
	@RequestMapping(value = "/takeout")
	public String takeout(@RequestParam(value = "code") String code,
			@RequestParam(value = "state", required = false) String state,
			HttpServletRequest httpRequest) {
		log.info("code=" + code + ",state=" + state);
		String openid = null;
		try {
			openid = wxHttpClient.getOpenId(code, state);
		} catch (Exception e) {
			return "redirect:/oauth/errmsg?msg=" + e.getMessage();
		}
		log.info("openId is " + openid);
		if (openid == null) {
			return "redirect:/oauth/error?type=1";
		}
		
		httpRequest.getSession().setAttribute("openId", openid);
		httpRequest.getSession().setAttribute("mid", state);
		
		// 请求开放平台查询会员卡信息
		WeChartMemberInfoQueryInput input = new WeChartMemberInfoQueryInput();
		input.setMid(state);
		input.setOpenId(openid);
		WeChartMemberInfoQueryOutput output = (WeChartMemberInfoQueryOutput) wizarposOpenRestClient.post(input, "/wechart/info/query", WeChartMemberInfoQueryOutput.class);
		if (output != null && "0".equals(output.getCode())) {
			WeChartMemberInfoQueryOutput.Result result = output.getResult();
			if (result != null) {
				httpRequest.getSession().setAttribute("mrtCardNo", result.getCardNo());
				httpRequest.getSession().setAttribute("mrtCardId",  result.getId());	
				if (StringUtils.isEmpty(result.getUserName()) || StringUtils.isEmpty(result.getMobileNo())) {
					// 会员卡存在未激活，用户名和手机号有空值，激活会员卡
					return "redirect:/vip/receiveCard?mid=" + state;
				}
			}
		}
		
		return "redirect:/takeout/tomenu?mid=" + state;
	}

	/**
	 * 外卖（新版）入口
	 * 
	 * @param code
	 * @param state
	 * @return
	 */
	@RequestMapping(value = "/waimai")
	public String waimai(@RequestParam(value = "code") String code,
			@RequestParam(value = "state", required = false) String state,
			HttpServletRequest httpRequest) {
		log.info("code=" + code + ",state=" + state);
		String openid = null;
		try {
			openid = wxHttpClient.getOpenId(code, state);
		} catch (Exception e) {
			return "redirect:/oauth/errmsg?msg=" + e.getMessage();
		}
		log.info("openId is " + openid);
		if (openid == null) {
			return "redirect:/oauth/error?type=1";
		}
		
		httpRequest.getSession().setAttribute("openId", openid);
		httpRequest.getSession().setAttribute("mid", state);
		// 请求开放平台查询会员卡信息
		WeChartMemberInfoQueryInput input = new WeChartMemberInfoQueryInput();
		input.setMid(state);
		input.setOpenId(openid);
		WeChartMemberInfoQueryOutput output = (WeChartMemberInfoQueryOutput) wizarposOpenRestClient.post(input, "/wechart/info/query", WeChartMemberInfoQueryOutput.class);
		if (output == null) { // 系统异常
			return "redirect:/oauth/error?type=9";
		} else if ("0".equals(output.getCode())) {
			WeChartMemberInfoQueryOutput.Result result = output.getResult();
			if (result != null) {
				httpRequest.getSession().setAttribute("mrtCardNo", result.getCardNo());
				httpRequest.getSession().setAttribute("mrtCardId",  result.getId());	
				if (StringUtils.isEmpty(result.getUserName()) || StringUtils.isEmpty(result.getMobileNo())) {
					// 会员卡存在未激活，用户名和手机号有空值，激活会员卡
					return "redirect:/vip/receiveCard?mid=" + state;
				}
			} 
		}  else if ("60007".equals(output.getCode())) {// 60007	会员不存在
			// 会员卡不存在，领取会员卡
			return "redirect:/vip/receiveCard?mid=" + state;
		} 
		
		return "redirect:/waimai/tomenu?mid=" + state;
	}
	
	/**
	 * The enter of service calling for WeChart menu setting
	 * 
	 * @param code
	 * @param state
	 * @return
	 */
	@RequestMapping(value = "/hujiao")
	public String hujiao(@RequestParam(value = "code") String code,
			@RequestParam(value = "state", required = false) String state,
			HttpServletRequest httpRequest) {
		log.info("code=" + code + ",state=" + state);
		String openid = null;
		try {
			openid = wxHttpClient.getOpenId(code, state);
		} catch (Exception e) {
			return "redirect:/oauth/errmsg?msg=" + e.getMessage();
		}
		log.info("openId is " + openid);
		if (openid == null) {
			return "redirect:/oauth/error?type=1";
		}
		
		httpRequest.getSession().setAttribute("openId", openid);
		httpRequest.getSession().setAttribute("mid", state);
		
		// 请求开放平台查询会员卡信息
		WeChartMemberInfoQueryInput input = new WeChartMemberInfoQueryInput();
		input.setMid(state);
		input.setOpenId(openid);
		WeChartMemberInfoQueryOutput output = (WeChartMemberInfoQueryOutput) wizarposOpenRestClient.post(input, "/wechart/info/query", WeChartMemberInfoQueryOutput.class);
		if (output != null && "0".equals(output.getCode())) {
			WeChartMemberInfoQueryOutput.Result result = output.getResult();
			if (result != null) {
				httpRequest.getSession().setAttribute("mrtCardNo", result.getCardNo());
				httpRequest.getSession().setAttribute("mrtCardId",  result.getId());	
				if (StringUtils.isEmpty(result.getUserName()) || StringUtils.isEmpty(result.getMobileNo())) {
					// 会员卡存在未激活，用户名和手机号有空值，激活会员卡
					return "redirect:/vip/receiveCard?mid=" + state;
				}
			} else {
				return "redirect:/oauth/error?type=9";// 其他错误，系统异常
			}
		} else if (output == null) {// 请求开放平台失败
			return "redirect:/oauth/error?type=9";// 其他错误，系统异常
		} else if ("60001".equals(output.getCode())) {// 60001 	非法慧商户号
			return "redirect:/oauth/error?type=7";// 未知商户
		} else if ("60007".equals(output.getCode())) {// 60007	会员不存在
			//若无会员卡，则前往会员卡领取页面领取会员卡
			return "redirect:/vip/receiveCard?mid=" + state;
		} else {
			// 60002	在此商户下不存在此会员
			// 60009	入参为空
			log.error("code: " + output.getCode());
			log.error("message: " + output.getMessage());
			return "redirect:/oauth/error?type=9";// 其他错误，系统异常
		}
		
		return "redirect:/dgd/tohujiao?mid=" + state;
	}
	
	/**
	 * 餐饮点单详情
	 * 
	 * @param code
	 * @param state
	 * @return
	 */
	@RequestMapping(value = "/catering_menuinfo")
	public String cateringMenuInfo(@RequestParam(value = "code") String code,
			@RequestParam(value = "state", required = false) String state,
			HttpServletRequest httpRequest) {
		log.info("code=" + code + ",state=" + state);
		int pos = state.indexOf("ORDERID");
		String mid = state.substring(0, pos);
		String orderId = state.substring(pos + 7);
		String openid = null;
		try {
			openid = wxHttpClient.getOpenId(code, mid);
		} catch (Exception e) {
			return "redirect:/oauth/errmsg?msg=" + e.getMessage();
		}
		log.info("openId is " + openid);
		if (openid == null) {
			return "redirect:/oauth/error?type=1";
		}
		
		httpRequest.getSession().setAttribute("openId", openid);
		httpRequest.getSession().setAttribute("mid", mid);
		
		// 请求开放平台查询会员卡信息
		WeChartMemberInfoQueryInput input = new WeChartMemberInfoQueryInput();
		input.setMid(mid);
		input.setOpenId(openid);
		WeChartMemberInfoQueryOutput output = (WeChartMemberInfoQueryOutput) wizarposOpenRestClient.post(input, "/wechart/info/query", WeChartMemberInfoQueryOutput.class);
		if (output != null && "0".equals(output.getCode())) {
			WeChartMemberInfoQueryOutput.Result result = output.getResult();
			if (result != null) {
				httpRequest.getSession().setAttribute("mrtCardNo", result.getCardNo());
				httpRequest.getSession().setAttribute("mrtCardId",  result.getId());	
				if (StringUtils.isEmpty(result.getUserName()) || StringUtils.isEmpty(result.getMobileNo())) {
					// 会员卡存在未激活，用户名和手机号有空值，激活会员卡
					return "redirect:/vip/receiveCard?mid=" + mid;
				}
			} else {
				return "redirect:/oauth/error?type=9";// 其他错误，系统异常
			}
		} else if (output == null) {// 请求开放平台失败
			return "redirect:/oauth/error?type=9";// 其他错误，系统异常
		} else if ("60001".equals(output.getCode())) {// 60001 	非法慧商户号
			return "redirect:/oauth/error?type=7";// 未知商户
		} else if ("60007".equals(output.getCode())) {// 60007	会员不存在
			//若无会员卡，则前往会员卡领取页面领取会员卡
			return "redirect:/vip/receiveCard?mid=" + state;
		} else {
			// 60002	在此商户下不存在此会员
			// 60009	入参为空
			log.error("code: " + output.getCode());
			log.error("message: " + output.getMessage());
			return "redirect:/oauth/error?type=9";// 其他错误，系统异常
		}
		
		return "redirect:/dgd/order_detail?mid=" + mid + "&orderId=" + orderId;
	}
	
	/**
	 * catering order list
	 * 
	 * @param code
	 * @param state
	 * @return
	 */
	@RequestMapping(value = "/catering_list")
	public String cateringList(@RequestParam(value = "code") String code,
			@RequestParam(value = "state", required = false) String state,
			HttpServletRequest httpRequest) {
		log.info("code=" + code + ",state=" + state);
		String openid = null;
		try {
			openid = wxHttpClient.getOpenId(code, state);
		} catch (Exception e) {
			return "redirect:/oauth/errmsg?msg=" + e.getMessage();
		}
		log.info("openId is " + openid);
		if (openid == null) {
			return "redirect:/oauth/error?type=1";
		}
		
		httpRequest.getSession().setAttribute("openId", openid);
		httpRequest.getSession().setAttribute("mid", state);
		
		// 请求开放平台查询会员卡信息
		WeChartMemberInfoQueryInput input = new WeChartMemberInfoQueryInput();
		input.setMid(state);
		input.setOpenId(openid);
		WeChartMemberInfoQueryOutput output = (WeChartMemberInfoQueryOutput) wizarposOpenRestClient.post(input, "/wechart/info/query", WeChartMemberInfoQueryOutput.class);
		if (output != null && "0".equals(output.getCode())) {
			WeChartMemberInfoQueryOutput.Result result = output.getResult();
			if (result != null) {
				httpRequest.getSession().setAttribute("mrtCardNo", result.getCardNo());
				httpRequest.getSession().setAttribute("mrtCardId",  result.getId());	
				if (StringUtils.isEmpty(result.getUserName()) || StringUtils.isEmpty(result.getMobileNo())) {
					// 会员卡存在未激活，用户名和手机号有空值，激活会员卡
					return "redirect:/vip/receiveCard?mid=" + state;
				}
			} else {
				return "redirect:/oauth/error?type=9";// 其他错误，系统异常
			}
		} else if (output == null) {// 请求开放平台失败
			return "redirect:/oauth/error?type=9";// 其他错误，系统异常
		} else if ("60001".equals(output.getCode())) {// 60001 	非法慧商户号
			return "redirect:/oauth/error?type=7";// 未知商户
		} else if ("60007".equals(output.getCode())) {// 60007	会员不存在
			//若无会员卡，则前往会员卡领取页面领取会员卡
			return "redirect:/vip/receiveCard?mid=" + state;
		} else {
			// 60002	在此商户下不存在此会员
			// 60009	入参为空
			log.error("code: " + output.getCode());
			log.error("message: " + output.getMessage());
			return "redirect:/oauth/error?type=9";// 其他错误，系统异常
		}
		
		return "redirect:/catering/myorders?mid=" + state;
	}
	
	/**
	 * catering reorder list.
	 * 餐饮预订单list查询
	 * 
	 * @param code
	 * @param state
	 * @return
	 */
	@RequestMapping(value = "/catering_re_list")
	public String cateringReList(@RequestParam(value = "code") String code,
			@RequestParam(value = "state", required = false) String state,
			HttpServletRequest httpRequest) {
		log.info("code=" + code + ",state=" + state);
		String openid = null;
		try {
			openid = wxHttpClient.getOpenId(code, state);
		} catch (Exception e) {
			return "redirect:/oauth/errmsg?msg=" + e.getMessage();
		}
		log.info("openId is " + openid);
		if (openid == null) {
			return "redirect:/oauth/error?type=1";
		}
		
		httpRequest.getSession().setAttribute("openId", openid);
		httpRequest.getSession().setAttribute("mid", state);
		
		// 请求开放平台查询会员卡信息
		WeChartMemberInfoQueryInput input = new WeChartMemberInfoQueryInput();
		input.setMid(state);
		input.setOpenId(openid);
		WeChartMemberInfoQueryOutput output = (WeChartMemberInfoQueryOutput) wizarposOpenRestClient.post(input, "/wechart/info/query", WeChartMemberInfoQueryOutput.class);
		if (output != null && "0".equals(output.getCode())) {
			WeChartMemberInfoQueryOutput.Result result = output.getResult();
			if (result != null) {
				httpRequest.getSession().setAttribute("mrtCardNo", result.getCardNo());
				httpRequest.getSession().setAttribute("mrtCardId",  result.getId());	
				if (StringUtils.isEmpty(result.getUserName()) || StringUtils.isEmpty(result.getMobileNo())) {
					// 会员卡存在未激活，用户名和手机号有空值，激活会员卡
					return "redirect:/vip/receiveCard?mid=" + state;
				}
			} else {
				return "redirect:/oauth/error?type=9";// 其他错误，系统异常
			}
		} else if (output == null) {// 请求开放平台失败
			return "redirect:/oauth/error?type=9";// 其他错误，系统异常
		} else if ("60001".equals(output.getCode())) {// 60001 	非法慧商户号
			return "redirect:/oauth/error?type=7";// 未知商户
		} else if ("60007".equals(output.getCode())) {// 60007	会员不存在
			//若无会员卡，则前往会员卡领取页面领取会员卡
			return "redirect:/vip/receiveCard?mid=" + state;
		} else {
			// 60002	在此商户下不存在此会员
			// 60009	入参为空
			log.error("code: " + output.getCode());
			log.error("message: " + output.getMessage());
			return "redirect:/oauth/error?type=9";// 其他错误，系统异常
		}
		
		return "redirect:/catering/myreorders?mid=" + state;
	}
	
//	/**
//	 * 测试为观注公众号情况下是否可以完成微信支付
//	 */
//	@RequestMapping(value = "/test")
//	public String test(@RequestParam(value = "code") String code,
//			@RequestParam(value = "state", required = false) String state) {
//		log.info("code=" + code + ",state=" + state);
//		String openid = wxHttpClient.getOpenId(code, state);
//		log.info("openId is " + openid);
//		if (openid == null) {
//			return "redirect:/oauth/error?type=1";
//		}
//		return "redirect:/tenpay/takeout?orderId=5054193f-2462-456b-98fc-4a0a8c7e80c1&openId=" + openid + "&mid=" + state;
//	}
	
	/**
	 * demo for zhanhui
	 * 
	 * @param code
	 * @param state
	 * @return
	 */
	@RequestMapping(value = "/zhanhui")
	public String zhanhui(@RequestParam(value = "code") String code,
			@RequestParam(value = "state", required = false) String state) {
		log.info("code=" + code + ",state=" + state);
		String openid = null;
		try {
			openid = wxHttpClient.getOpenId(code, state);
		} catch (Exception e) {
			return "redirect:/oauth/errmsg?msg=" + e.getMessage();
		}
		log.info("openId is " + openid);
		if (openid == null) {
			return "redirect:/oauth/error?type=1";
		}
		MrtIbeaconInput input = new MrtIbeaconInput();
		input.setMid(state);
		MrtIbeaconOutput output = (MrtIbeaconOutput) wizarposOpenRestClient.post(input, "/wxshop/ineacon/info", MrtIbeaconOutput.class);		
		MrtIbeacon scene = null;
		if (output == null || !SysConstants.OPEN_ERROR.equals(output.getCode())) {
			return "redirect:/oauth/error?type=0";
		} else if (SysConstants.NO_MERCHANT_CODE.equals(output.getCode())) {
			return "redirect:/oauth/error?type=7";
		} else {
			scene = output.getResult();
		}
//		MrtIbeacon scene = ibeaconService.getActive(state);// ibeacon 场景设置
		if (scene == null) {
			return "redirect:/oauth/error?type=2";
		}
		
		String content = scene.getContent();// 场景内容
		String type = scene.getType(); // 场景类型
		if (content == null || content.isEmpty() || type == null
				|| type.isEmpty()) {
			return "redirect:/oauth/error?type=2";
		}

		if (SIGLE.equals(type)) {// 单张券
//			return "redirect:/zhanhui/index528?mid=" + state + "&openId=" + openId + "&ticketDefId=" + content;
			return "redirect:/zhanhui/index612?wechat_card_js=1&mid=" + state + "&openId=" + openid + "&ticketDefId=" + content;
		} else if (ADVERTISEMENT.equals(type)) {// 广告投放
			return "redirect:" + content;
		}
		
		return "redirect:/oauth/error?type=5";
	}

	/**
	 * dongzhixing H5页面摇一摇周边
	 * 
	 * @param code
	 * @param state
	 * @return
	 */
	@RequestMapping(value = "/shake")
	public String shake(@RequestParam(value = "ticket") String ticket,
			@RequestParam(value = "mid", required = true) String mid) {
		log.info("ticket=" + ticket + ",mid=" + mid);
		String openid = null;
		try {
			openid = wxHttpClient.getShakeOpenId(ticket, mid);
		} catch (Exception e) {
			return "redirect:/oauth/errmsg?msg=" + e.getMessage();
		}
		log.info("openId is " + openid);
		if (openid == null) {
			return "redirect:/oauth/error?type=1";
		}
		return "redirect:/ibeacon/shake?openId=" + openid + "&mid=" + mid;
	}
	
	/**
	 * 东志信  H5页面摇一摇周边二维码版
	 * 
	 * @param code
	 * @param state
	 * @return
	 */
	@RequestMapping(value = "/qr_shake_zb")
	public String qrShakeZb(@RequestParam(value = "ticket") String ticket,
			@RequestParam(value = "mid", required = true) String mid) {
		log.info("ticket=" + ticket + ",mid=" + mid);
		String openid = null;
		try {
			openid = wxHttpClient.getShakeOpenId(ticket, mid);
		} catch (Exception e) {
			return "redirect:/oauth/errmsg?msg=" + e.getMessage();
		}
		log.info("openId is " + openid);
		if (openid == null) {
			return "redirect:/oauth/error?type=1";
		}
		return "redirect:/ibeacon/qr_shake?openId=" + openid + "&mid=" + mid;
	}
	
	/**
	 * 东志信  H5页面摇一摇二维码版
	 * 
	 * @param code
	 * @param state
	 * @return
	 */
	@RequestMapping(value = "/qr_shake")
	public String qrShake(@RequestParam(value = "code") String code,
			@RequestParam(value = "state", required = false) String state) {
		log.info("code=" + code + ",state=" + state);
		String openid = null;
		try {
			openid = wxHttpClient.getOpenId(code, state);
		} catch (Exception e) {
			return "redirect:/oauth/errmsg?msg=" + e.getMessage();
		}
		log.info("openId is " + openid);
		if (openid == null) {
			return "redirect:/oauth/error?type=1";
		}
		return "redirect:/ibeacon/qr_shake?openId=" + openid + "&mid=" + state;
	}
	
	/**
	 * 微信jsapi支付入口
	 * 
	 * @param code
	 * @param state
	 * @return
	 */
	@RequestMapping(value = "/jsapi_pay")
	public String jsApiPay(@RequestParam(value = "code") String code,
			@RequestParam(value = "state", required = false) String state,
			HttpServletRequest httpRequest) {
		// des解密 id 取得 mid,sn
		String mid = null;
		String sn = null;
		String orderId = null;
		String plainText = null;
		try {
			plainText = DES.decryptDES(state, SysConstants.ENCRYPT_DECRYPT_KEY);
		} catch (Exception e) {
			e.printStackTrace();
			log.error("解密失败");
			log.error(state);
			log.error(plainText);
			return "redirect:/oauth/errmsg?msg=" + e.getMessage();
		}

		JSONObject param = JSONObject.parseObject(plainText);
		mid = param.getString("mid");
		sn = param.getString("sn");
		orderId = param.getString("orderId");
		
		String openid = null;
		try {
			openid = wxHttpClient.getOpenIdForPay(code, mid);
		} catch (Exception e) {
			return "redirect:/oauth/errmsg?msg=" + e.getMessage();
		}
		log.info("openId is " + openid);
		if (openid == null) {
			return "redirect:/oauth/error?type=1";
		}
		
		httpRequest.getSession().setAttribute("openId", openid);
		httpRequest.getSession().setAttribute("mid", mid);
		
		// 请求开放平台查询会员卡信息
		WeChartMemberInfoQueryInput input = new WeChartMemberInfoQueryInput();
		input.setMid(mid);
		input.setOpenId(openid);
		WeChartMemberInfoQueryOutput output = (WeChartMemberInfoQueryOutput) wizarposOpenRestClient.post(input, "/wechart/info/query", WeChartMemberInfoQueryOutput.class);
		if (output != null && "0".equals(output.getCode()) && output.getResult() != null) {
			httpRequest.getSession().setAttribute("mrtCardNo", output.getResult().getCardNo());
			httpRequest.getSession().setAttribute("mrtCardId", output.getResult().getId());				
		} 
		
		return "redirect:/tenpay/pay_wx?mid=" + mid+"&sn="+sn+"&orderId="+orderId;
	}

}
